package A1_贪心算法;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC_56合并区间1 {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals,(a,b)->a[0]-b[0]);
        List<int[]> result=new ArrayList<>();
        result.add(intervals[0]);
        for(int i=1;i<intervals.length;i++){
            int size=result.size();
            if(size>0 && intervals[i][0]<=result.get(size-1)[1])
                result.get(size-1)[1]=Math.max(intervals[i][1],result.get(size-1)[1]);
            else
                result.add(intervals[i]);
        }
        return result.toArray(new int[result.size()][2]);
    }
}
